/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import controller.Action;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Cliente;
import persistence.ClienteDAO;

    
    
/**
 *
 * @author Cristian
 */
public class GravarClienteAction implements Action{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IIOException {
        String nomeCliente = request.getParameter("nomeCliente");
        String cpf = request.getParameter("cpf");
        String rg = request.getParameter("rg");
        String telefone = request.getParameter("telefone");
        
        if(nomeCliente.equals("") || cpf.equals("") ||  rg.equals("") || 
                telefone.equals("")) {
            try {
                response.sendRedirect("index.jsp");
            } catch (IOException ex) {
                Logger.getLogger(GravarClienteAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
              Cliente cliente = new Cliente();
              cliente.setNome(nomeCliente);
              cliente.setCpf(cpf);
              cliente.setRg(rg);
              cliente.setTelefone(telefone);
              ClienteDAO.getInstace().save(cliente);
              response.sendRedirect("cadastroRealizado.jsp");
                
            } catch (Exception e) {
                try {
                    response.sendRedirect("cadastroErro.jsp");
                } catch (IOException ex) {
                    Logger.getLogger(GravarClienteAction.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
         
        }
    }
    }    
    

